#!/usr/bin/python
###################################################################################################                                                                                              
#                                                                                               
# DEVELOPERS : Luca Sanna                                                                        
# VERSIONE : 0.1                                                                                  
#                                                                                                                    
# E-MAIL: sanluca78@gmail.com                                                                                        
# COPYRIGHT: (c) 2015                                                                       
# PROGRAMMA: Py-Home-Master  
#This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.                                                                          
###################################################################################################

import sys,time
from threading import Thread
from baseconfig import *
sys.path.append("%slib" %workingDir)
#db
from database import *
from pub import *
from subclass import *

try:
    import pygtk
    pygtk.require("2.0")
except:
    pass
try:
    #import gtk
    #import gtk.glade
    from gi.repository import Gtk
except:
    sys.exit(1)
    
import paho.mqtt.client as mqtt

class guiHome():
    db=Mydata()
    mq=MyPub()
    #sub=MyMQTTClass()

    def __init__(self):
        
        #avvio thread subclass
        
        #self.sub.start()
        
        #Set the Glade file
        gladefile = "gui/py-home.glade"  
        
        #Create our dictionay and connect it
        dic = { "click_apri_tenda" : self.apri_tenda,
            "click_chiudi_tenda" : self.chiudi_tenda,
            "click_meta_tenda" : self.meta_tenda,
            "click_luce_soggiorno" : self.luce_soggiorno,
            "click_luce_cucina" : self.luce_cucina,
            "click_luce_corridoio1" : self.luce_corridoio1,
            "click_luce_corridoio2" : self.luce_corridoio2,
            "click_luce_studio" : self.luce_studio,
            "click_luce_spegni_tutto" : self.luce_spegni_tutto,
            "click_caldaia" : self.caldaia }
        
        self.builder=Gtk.Builder()
        self.builder.add_from_file(gladefile)
        self.builder.connect_signals(dic)
        
        try:
            self.view_temp_umid_soggiorno()
            self.view_temp_umid_cucina()
            self.view_temp_umid_studio()
            self.view_temp_caldaia()
            self.view_temp_esterna()
        except:
            pass
            
        
        window=self.builder.get_object("py-home")
        window.show_all()
        
    def caldaia(self,widget):
        spin1=self.builder.get_object("spinbutton1")
        spin=int(spin1.get_value())
        self.mq.pub('home/caldaia/temperatura/impostata',spin)
        #self.db.db64_update_boiler('temp_imp',spin,1)
        
    def view_temp_caldaia(self):
        a=self.db.view_real_time('caldaia')
        temp=a[1]
        entryForText = self.builder.get_object("textview5")
        entryForText.get_buffer().set_text(str(temp))

    def view_temp_umid_soggiorno(self):
        a=self.db.view_real_time('soggiorno')
        temp=a[1]
        umid=a[2]
        entryForText = self.builder.get_object("textview1")
        entryForText.get_buffer().set_text(str(temp))
        
        entryForText1 = self.builder.get_object("textview2")
        entryForText1.get_buffer().set_text(str(umid))
        
    def view_temp_umid_cucina(self):
        a=self.db.view_real_time('cucina')
        temp=a[1]
        umid=a[2]
        entryForText = self.builder.get_object("textview3")
        entryForText.get_buffer().set_text(str(temp))
        
        entryForText1 = self.builder.get_object("textview4")
        entryForText1.get_buffer().set_text(str(umid))
        
    def view_temp_umid_studio(self):
        a=self.db.view_real_time('studio')
        temp=a[1]
        umid=a[2]
        entryForText = self.builder.get_object("textview7")
        entryForText.get_buffer().set_text(str(temp))
        
        entryForText1 = self.builder.get_object("textview8")
        entryForText1.get_buffer().set_text(str(umid))
        
    def view_temp_esterna(self):
        a=self.db.view_real_time('tenda')
        temp=a[1]
        entryForText = self.builder.get_object("textview6")
        entryForText.get_buffer().set_text(str(temp))
        
    def apri_tenda(self,widget):
        self.mq.pub('home/tenda/posizione','APRI')
        time.sleep(2)
    
    def chiudi_tenda(self,widget):
        self.mq.pub('home/tenda/posizione','CHIUDI')
        time.sleep(2)
        
    def meta_tenda(self,widget):
        self.mq.pub('home/tenda/posizione','META')
        time.sleep(2)
    
    def luce_soggiorno(self,widget):
        self.mq.pub('home/soggiorno/luce','1')
        time.sleep(2)
        
    def luce_cucina(self,widget):
        self.mq.pub('home/cucina/luce','2')
        time.sleep(2)
        
    def luce_corridoio1(self,widget):
        self.mq.pub('home/cucina/luce','1')
        time.sleep(2)
    
    def luce_corridoio2(self,widget):
        self.mq.pub('home/studio/luce','1')
        time.sleep(2)
            
    def luce_studio(self,widget):
        self.mq.pub('home/studio/luce','2')
        time.sleep(2)
            
    def luce_spegni_tutto(self,widget):
        self.mq.pub('home/studio/luce','TUTTO')
        self.mq.pub('home/cucina/luce','TUTTO')
        self.mq.pub('home/soggiorno/luce','TUTTO')
        time.sleep(2)

if __name__ == "__main__":
    hwg = guiHome()
    Gtk.main()
    #self.sub.start()
